*******************************************************************************************************************************
* This do file prepares data for industry-level analysis.
* Last modified by: Yukun Wang
* Date:02/02/2017
*******************************************************************************************************************************

cap log close
clear all
set more off
set scheme s1color

***Set directory
capture cd "/Users/Dropbox/Chinese food exports"

***
use "Data/ChineseCustomsData/customs_yearly_2000_2013.dta", clear
compress
rename hs hs6
gen hs2_correct=substr(hs_id,1,2)
destring hs2, replace

keep if import==0   //keep only export data

sort party_id hs_id
merge m:1 party_id hs_id using "Data/inspection_lists/contaminated_list_full.dta"
tab _m
drop if _m==2
g affected=(_m==3)
drop _m

drop unit_id city_id type_id party_id shipment_id trans_id interm type_id2 hs2

rename hs2_correct hs2

***mark dairy products with hs=100
replace hs2=100 if hs_id=="04011000" | hs_id=="04012000" | hs_id=="04013000" |    ///
hs_id=="04014000" | hs_id=="04015000" | hs_id=="04021000" | hs_id=="04022100" |    ///
hs_id=="04022900" | hs_id=="04029100" | hs_id=="04029900" | hs_id=="04031000" |    ///
hs_id=="04039000" | hs_id=="04041000" | hs_id=="04049000" | hs_id=="04051000" |    ///
hs_id=="04052000" | hs_id=="04059000" | hs_id=="04061000" | hs_id=="04062000" |     ///
hs_id=="04063000" | hs_id=="04064000" | hs_id=="04069000" | hs_id=="19011000" |    ///
hs_id=="19019000" | hs_id=="35011000" | hs_id=="35022000"

replace hs6=999999 if hs2==100
***Create Food Codebook
merge m:1 hs_id using "Data/product_codebook/HS_code_for_food.dta"
drop if _m==2
g food=(_m==3)
drop _m
drop description

***calculate export value share to different continent 
merge m:1 origin_id using "Data/country_codebook/country_continent_id"
tab _m
drop if _m==2
replace continent_id=7 if _m==1
drop _merge   

compress

save "Data/Coded data/Customs/synth_firmhs8year_beforecollapsed", replace

************************************************
*Part 1: Create various synthetic control data *
*	- With all firm-products
*	- Exluding affected firm-products
*************************************************

set more off

forvalues i=0/1{

	***collapse to hs-year level
	use "Data/Coded data/Customs/synth_firmhs8year_beforecollapsed",clear

	//keep if food==0|treated==1 	//excluding food categories

	drop if affected==1 & `i'==1	//including or excluding affected firm-product

	keep hs2 year continent_id value food
	preserve
		keep food hs
		collapse (mean) food, by(hs)
		assert food!=.
		replace food=1 if inrange(food, 0.3, 1)
		replace food=0 if inrange(food, 0, 0.29)
		sort hs
		sa Data/Codebooks/food_hs2, replace
	restore
	collapse (sum) value , by (hs year continent_id)

	bys hs year: egen totalvalue=sum(value)
	g value_share=value/totalvalue

	keep hs year continent_id totalvalue value_share
	ren totalvalue value

	reshape wide value_share,i(hs year value) j(continent_id)

	foreach v of varlist value_share*{

		replace `v'=0 if `v'==.
		
	}

	fillin hs year
	replace value=0 if _fillin==1
	g entry_hsyear=1-_fillin
	drop _fillin

	sort hs year

	g lvalue=log(value+0.1)

	***rename variable label
	label var value_share1 "value export to Asia"
	label var value_share2 "value export to Europe"
	label var value_share3 "value export to Africa"
	label var value_share4 "value export to Oceania"
	label var value_share5 "value export to North America"
	label var value_share6 "value export to Latin America"
	label var value_share7 "value export to missing destinations"
	label var year "year"
	label var hs "2-digit hs code"
	label var value "annual value"
	label var lvalue "log value"
	
	cap drop food
	merge m:1 hs using Data/Codebooks/food_hs2
	drop if _m==2
	drop _m
	save "Data/Coded data/Customs/synth_hs2year_dropaffected`i'_collapsed", replace

}


exit, clear
